package com.wanmait.recruitment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recruitment.mapper.ConfigMapper;
import com.wanmait.recruitment.mapper.JobMapper;
import com.wanmait.recruitment.pojo.*;
import com.wanmait.recruitment.service.JobService;
import com.wanmait.recruitment.util.PageContext;
import com.wanmait.recruitment.util.RelativeDateFormat;
import org.eclipse.jdt.internal.compiler.env.ISourceType;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class JobServiceImpl implements JobService {
    @Resource
    private JobMapper jobMapper;
    @Resource
    private ConfigMapper configMapper;

    @Override
    @Cacheable("jobCache")
    public List<Job> selectJobList(int count,String sign) {
        List<Job>jobs = jobMapper.selectJobList(count, sign);
        for (Job job :jobs)
        {
            job.setTime(RelativeDateFormat.format(job.getInputTime()));
        }
        return jobs;
    }


    @Override
    public void insert(Job object) {

    }

    @Override
    @CacheEvict(value = "jobCache",allEntries = true)
    public void delete(Integer id) {
        jobMapper.deleteJob(id);
    }

    @Override
    public void delete(String ids) {

    }

    @Override
    @CacheEvict(value = "jobCache",allEntries = true)
    public void update(Job object) {
        jobMapper.updateJob(object);
    }

    @Override
    public Job findById(Integer id) {
        return jobMapper.findById(id);
    }


    @Override
    @Cacheable("jobCache")
    public  List<Job>findAllById(Integer id){
        return  jobMapper.selectById(id);
    }
    @Override
    public  List<Job>findCompanyById(Integer id){
        return  jobMapper.findByCompany(id);
    }
    @Override
    public  List<Job>findCompanyByIdTop3(Integer id){
        return  jobMapper.findByCompanyTop3(id);
    }

    @Override
    @CacheEvict(value = "jobCache",allEntries = true)
    public int insertList(Job job) {
        return jobMapper.insertList(job);
    }

    @Override
    public List<Job> findAll() {
        return null;
    }

    @Override
    @Cacheable("jobCache")
    public List<Job> findAll(Job object) {
        return jobMapper.selectList(object);
    }

    @Override
    public PageInfo<Job> findPage(int pageNum, int pageSize) {
        return null;
    }

    @Override
    @Cacheable("jobCache")
    public PageInfo<Job> findPage(int pageNum, int pageSize, Integer id) {
        PageHelper.startPage(pageNum,pageSize);
        List<Job>jobList=jobMapper.selectById(id);
        PageInfo<Job>pageInfo=new PageInfo<>(jobList);
        return pageInfo;
    }

    @Override
    @Cacheable("jobCache")
    public PageInfo<Job> findPageJob(Job job, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Job> jobs = jobMapper.selectList(job);
        PageInfo<Job> pageInfo = new PageInfo<>(jobs);
        return pageInfo;
    }

    @Override
    @Cacheable("jobCache")
    public PageInfo<Job> findByCompany(Integer id,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Job>jobs =  jobMapper.findByCompany(id);
        PageInfo<Job> pageInfo = new PageInfo<>(jobs);
        return  pageInfo;
    }

    @Override
    public int findByCompanyCount(Integer id) {
        return jobMapper.findByCompanyCount(id);
    }

    @Override
    @CacheEvict(value = "jobCache",allEntries = true)
    public int OfflineJob(Integer id) {
        return jobMapper.OfflineJob(id);
    }

    @Override
    @Cacheable("jobCache")
    public List<Job> findByCompanyId(Integer id) {
        return jobMapper.findByCompany(id);
    }

    @Override
    @Cacheable("jobCache")
    public PageInfo<Job> findRecommend(List<Subscribe> subscribes) {
        Map map = new HashMap<>();
        List<Industry> industryJobs = new ArrayList<>();
        List<City> cities = new ArrayList<>();
        List<Stage> stages = new ArrayList<>();
        List<Industry> industries = new ArrayList<>();
        List<Money> monies = new ArrayList<>();
        for (Subscribe subscribe:subscribes) {
            industryJobs.add(subscribe.getIndustryJob());
            cities.add(subscribe.getCity());
            stages.add(subscribe.getStage());
            industries.add(subscribe.getIndustry());
            monies.add(subscribe.getMoney());
        }
        map.put("industryJobs", industryJobs);
        map.put("cities",cities);
        map.put("stages",stages);
        map.put("industries",industries);
        map.put("monies",monies);
        Integer pageNum = PageContext.getPageNum();
        Integer pageSize = 0;
        try {
            pageSize = Integer.parseInt(configMapper.getValueByKey("recommended_count"));
        } catch (NumberFormatException e) {
            pageSize = Integer.parseInt(configMapper.getDefaultValueByKey("recommended_count"));
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Job> jobs = jobMapper.selectRecommended(map);
        PageInfo<Job> pageInfo = new PageInfo<>(jobs);
        return pageInfo;
    }

    @Override
    public List<Job> findAllJobName() {
        return jobMapper.findAllJobName();
    }

    @Override
    @Cacheable("jobCache")
    public PageInfo<Job> findOffline(Integer id,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Job>jobs = jobMapper.findOffline(id);
        PageInfo<Job>pageInfo = new PageInfo<>(jobs);
        return pageInfo;
    }

    @Override
    public int findByofflineCount(Integer id) {
        return jobMapper.findByofflineCount(id);
    }

    @Override
    @CacheEvict(value = "jobCache",allEntries = true)
    public int onlineJob(Integer id) {
        return jobMapper.onlineJob(id);
    }

    @Override
    @CacheEvict(value = "jobCache",allEntries = true)
    public Job findOfflineById(Integer id) {
        return jobMapper.findOfflineById(id);
    }

    @Override
    @CacheEvict(value = "jobCache",allEntries = true)
    public void updateResumeCount(Integer id,Integer change) {
        jobMapper.updateResumeCount(id,change);
    }

    @Override
    @CacheEvict(value = "jobCache",allEntries = true)
    public void updateLookCount(Integer id) {
        jobMapper.updateLookCount(id);
    }

}
